/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seagate.app.servlet;

import com.seagate.app.bean.BoBean;
import com.seagate.app.bean.RoomBean;
import com.seagate.app.manager.RoomManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 452530
 */
public class LoadRoomTable extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            if ("loadroom".equalsIgnoreCase(action)) {
                RoomManager rmg = new RoomManager();
                Map roommap = rmg.loadAllRoomPercentCap();
                out.println("");

                out.println("{");
                for (int i = 1; i <= 10; i++) {
                    String percentcap = (String) roommap.get("A" + i);
                    if (i > 1) {
                        out.println(",");
                    }

                    if (percentcap != null) {
                        int percen = (int) ((Double.parseDouble(percentcap) * 100) / 10);
                        double perc= Double.parseDouble(percentcap)* 100;
                        if(perc>0D && perc<10.0D ){
                         out.println("\"A" + i + "\":\"images/indicator/indicator" + 1 + "0.PNG\"");
                        }else {
                        out.println("\"A" + i + "\":\"images/indicator/indicator" + percen + "0.PNG\"");
                        }

                    } else {
                        out.println("\"A" + i + "\":\"images/indicator/indicator0.PNG\"");
                    }
                }
                for (int i = 1; i <= 10; i++) {

                    out.print(",");

                    String percentcap = (String) roommap.get("B" + i);
                    if (percentcap != null) {
                        int percen = (int) ((Double.parseDouble(percentcap) * 100) / 10);
                       double perc= Double.parseDouble(percentcap)* 100;
                        if(perc>0D && perc<10.0D ){
                         out.println("\"B" + i + "\":\"images/indicator/indicator" + 1 + "0.PNG\"");
                        }else {
                        out.println("\"B" + i + "\":\"images/indicator/indicator" + percen + "0.PNG\"");
                        }

                    } else {
                        out.println("\"B" + i + "\":\"images/indicator/indicator0.PNG\"");
                    }
                }
                for (int i = 1; i <= 10; i++) {

                    out.print(",");

                    String percentcap = (String) roommap.get("FA" + i);
                    if (percentcap != null) {
                        int percen = (int) ((Double.parseDouble(percentcap) * 100) / 10);

                        double perc= Double.parseDouble(percentcap)* 100;
                        if(perc>0D && perc<10.0D ){
                         out.println("\"FA" + i + "\":\"images/indicator/indicator" + 1 + "0.PNG\"");
                        }else {
                        out.println("\"FA" + i + "\":\"images/indicator/indicator" + percen + "0.PNG\"");
                        }

                    } else {
                        out.println("\"FA" + i + "\":\"images/indicator/indicator0.PNG\"");
                    }
                }
                for (int i = 1; i <= 10; i++) {

                    out.print(",");

                    String percentcap = (String) roommap.get("FB" + i);
                    if (percentcap != null) {
                        int percen = (int) ((Double.parseDouble(percentcap) * 100) / 10);
                        double perc= Double.parseDouble(percentcap)* 100;
                        if(perc>0D && perc<10.0D ){
                         out.println("\"FB" + i + "\":\"images/indicator/indicator" + 1 + "0.PNG\"");
                        }else {
                        out.println("\"FB" + i + "\":\"images/indicator/indicator" + percen + "0.PNG\"");
                        }
                        

                    } else {
                        out.println("\"FB" + i + "\":\"images/indicator/indicator0.PNG\"");
                    }
                }
                out.println("}");

            } else if ("loadroomqty".equalsIgnoreCase(action)) {
               RoomManager rmg = new RoomManager();
                //Map roommap = rmg.loadAllRoomPercentCap();
                out.println("");

                out.println("{");
                for (int i = 1; i <= 10; i++) {

                    if (i > 1) {
                        out.println(",");
                    }

                   int qty=rmg.getBoQtyByShelf("A" + i);
                        out.println("\"A" + i + "\":\""+qty+"\"");
                }
                for (int i = 1; i <= 10; i++) {

                    out.print(",");

                    int qty=rmg.getBoQtyByShelf("B" + i);
                        out.println("\"B" + i + "\":\""+qty+"\"");
                }
                for (int i = 1; i <= 10; i++) {

                    out.print(",");
                    int qty=rmg.getBoQtyByShelf("FA" + i);
                        out.println("\"FA" + i + "\":\""+qty+"\"");

                }
                for (int i = 1; i <= 10; i++) {

                    out.print(",");

                        int qty=rmg.getBoQtyByShelf("FB" + i);
                        out.println("\"FB" + i + "\":\""+qty+"\"");

                }
                out.println("}");
            } else if ("loadroominfo".equalsIgnoreCase(action)) {
                String shelfno = request.getParameter("shelf");
                RoomManager rmg = new RoomManager();
                RoomBean rb = rmg.loadRoomShelfInfo(shelfno);
                if (rb != null) {
                    out.println("{");
                    out.println("\"success\":true,");
                    out.println("\"shelfno\":\"" + rb.getShelf_no() + "\",");
                    out.println("\"productname\":\"" + rb.getProductname() + "\",");
                    out.println("\"tgatype\":\"" + rb.getTga_type() + "\",");
                    out.println("\"capacity\":\"" + rb.getCapacity() + "\",");
                    out.println("\"bondpad\":\"" + rb.getBond_pad() + "\"");
                    out.println("}");
                } else {
                    out.println("{");
                    out.println("\"success\":false");
                    out.println("}");
                }

            } else if ("loadroomtooltip".equalsIgnoreCase(action)) {
                String shelfno = request.getParameter("shelf");
                RoomManager rmg = new RoomManager();
                RoomBean rb = rmg.loadRoomShelfInfo(shelfno);
                if (rb != null) {
                    NumberFormat nf = NumberFormat.getInstance();
                    nf.setMaximumFractionDigits(2);

                    out.println("<div align=\"center\">");
                    out.println("Product :" + rb.getProductname() + "-" + rb.getTga_type());
                    out.println("<br/>");
                    out.println("Qty :" + nf.format(rmg.getRoomPercentCapByShelf(shelfno) * 100) + "%");
                    out.println("<table width=\"250px\" bgcolor=\"#ffffff\" style=\"color: #000000\">");
                    out.println("<tr bgcolor=\"#33FF99\"  >");
                    out.println("<td align=\"center\"> BO </td><td  align=\"center\">Part Number</td><td  align=\"center\">Number</td>");
                    out.println(" </tr>");

                    List bolist = rmg.loadAllRoomShelfBo(shelfno);
                    for (int i = 0; i < bolist.size(); i++) {
                        BoBean temp = (BoBean) bolist.get(i);
                        out.println(" <tr>");
                        out.println("     <td align=\"center\">" + temp.getBoname() + "  </td><td  align=\"center\">" + temp.getHga_partnum() + "</td><td  align=\"center\">" + temp.getQty() + "</td>");
                        out.println(" </tr>");
                    }

                    out.println("</table>");
                    out.println("</div>");
                } else {
                    out.println("No Data ...");
                }


            } else if ("loadshelftable".equalsIgnoreCase(action)) {



               // out.println("</tr>");
                String shelfno = request.getParameter("shelf");
                String edit = request.getParameter("edit");
                boolean canedit=false;
                if(edit!=null && edit.equalsIgnoreCase("Y")){
                    canedit=true;
                }
                RoomManager rmg = new RoomManager();
                RoomBean rb = rmg.loadRoomShelfInfo(shelfno);

                if (rb != null) {
                    NumberFormat nf = NumberFormat.getInstance();
                    nf.setMaximumFractionDigits(2);
                    out.println("<br/>");
                    out.println("<div style=\"width: 600px;\">");
                    out.println("Product :" + rb.getProductname() + "-" + rb.getTga_type());
                    out.println("<br/>");
                    out.println("Bond Pad :" + rb.getBond_pad() );
                    
                    out.println("Qty :"+rmg.getBoQtyByShelf(shelfno)+" in "+rb.getCapacity()+" :" + nf.format(rmg.getRoomPercentCapByShelf(shelfno) * 100) + "%");
                    out.println("<br/>");
                    String role = (String) request.getSession().getAttribute("Roles");
                    if("sa".equalsIgnoreCase(role)&&canedit)
                    out.println("<input name=\"imgEdit\" id=\"imgEdit\" src=\"./images/buttons/btn_empty.gif\" onclick=\"emptyShelf('"+shelfno+"')\"  border=\"0\"  type=\"image\">");

                    out.println("</div>");

                    out.println("<table bgcolor=\"#CCCCCC\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
                    out.println("<tr><td>");
                    out.println("<table bgcolor=\"#6699CC\" cellpadding=\"3\" cellspacing=\"0\" border=\"0\" width=\"100%\">");

                    out.println("<tr bgcolor=\"#eeeeee\">");
                    out.println("<td>");
                    out.println("<table bgcolor=\"#cccccc\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
                    out.println("<tr><td>");
                    out.println("<table bgcolor=\"#cccccc\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\" width=\"100%\">");

                    out.println("<tr bgcolor=\"#99CC66\">");

                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;BO NAME&nbsp;</font></td>");
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;HGA PARTNUM&nbsp;</font></td>");
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;TSR&nbsp;</font></td>");
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;TAB&nbsp;</font></td>");
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;QTY&nbsp;</font></td>");
                    if(canedit)
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;ACTION&nbsp;</font></td>");
                     out.println("</tr>");
                    List bolist = rmg.loadAllRoomShelfBo(shelfno);
                    for (int i = 0; i < bolist.size(); i++) {
                        BoBean temp = (BoBean) bolist.get(i);
                        if (i % 2 == 0) {
                            out.println("<tr bgcolor=\"#ffffff\">");
                        } else {
                            out.println("<tr bgcolor=\"#ccffff\">");
                        }
                        out.println("<td>&nbsp;" + temp.getBoname() + "&nbsp;</td>");
                        out.println("<td>&nbsp;" + temp.getHga_partnum() + "&nbsp;</td>");
                        out.println("<td>&nbsp;" + temp.getTsr() + "&nbsp;</td>");
                        out.println("<td>&nbsp;" + temp.getTab() + "&nbsp;</td>");
                        out.println("<td>&nbsp;" + temp.getQty() + "&nbsp;</td>");
                        
                        if(canedit)
                        out.println("<td><input name=\"imgEdit\" id=\"imgEdit\" src=\"./images/buttons/btn_edit.gif\" onclick=\"editBo('" + temp.getBoname() + "','"+shelfno+"')\"  border=\"0\"  type=\"image\"> | <input name=\"imgDel\" id=\"imgDel\" src=\"./images/buttons/btn_delete.gif\" onclick=\"deleteBo('" + temp.getBoname() + "','"+shelfno+"')\" border=\"0\" type=\"image\"></td>");

                        out.println("</tr>");
                    }
                    out.println("</table>");
                    out.println("</td></tr>");
                    out.println("</table>");
                    out.println("</td>");
                    out.println("</tr>");

                    out.println("</table>");
                    out.println("</td></tr>");
                    out.println("</table>");
                }





            } else if ("loadshelfrejecttable".equalsIgnoreCase(action)) {



                out.println("</tr>");
                String shelfno = request.getParameter("shelf");
                String edit = request.getParameter("edit");
                boolean canedit=false;
                if(edit!=null && edit.equalsIgnoreCase("Y")){
                    canedit=true;
                }
                RoomManager rmg = new RoomManager();
                RoomBean rb = rmg.loadRoomShelfInfo(shelfno);

                if (rb != null) {
                    NumberFormat nf = NumberFormat.getInstance();
                    nf.setMaximumFractionDigits(2);
                    out.println("<br/>");
                    out.println("<div style=\"width: 600px;\">");
                    out.println("Product :" + rb.getProductname() + "-" + rb.getTga_type());
                    out.println("<br/>");
                    out.println("Bond Pad :" + rb.getBond_pad() );
                    out.println("<br/>");
                    out.println("Qty :"+rmg.getBoQtyByShelf(shelfno)+" in "+rb.getCapacity()+" :" + nf.format(rmg.getRoomPercentCapByShelf(shelfno) * 100) + "%");
                    out.println("</div>");

                    out.println("<table bgcolor=\"#CCCCCC\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
                    out.println("<tr><td>");
                    out.println("<table bgcolor=\"#6699CC\" cellpadding=\"3\" cellspacing=\"0\" border=\"0\" width=\"100%\">");

                    out.println("<tr bgcolor=\"#eeeeee\">");
                    out.println("<td>");
                    out.println("<table bgcolor=\"#cccccc\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
                    out.println("<tr><td>");
                    out.println("<table bgcolor=\"#cccccc\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\" width=\"100%\">");

                    out.println("<tr bgcolor=\"#99CC66\">");

                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;BO NAME&nbsp;</font></td>");
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;HGA PARTNUM&nbsp;</font></td>");
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;TSR&nbsp;</font></td>");
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;TAB&nbsp;</font></td>");
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;QTY&nbsp;</font></td>");
                    if(canedit)
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;ACTION&nbsp;</font></td>");

                    out.println("</tr>");
                    List bolist = rmg.loadAllRoomShelfBo(shelfno);
                    for (int i = 0; i < bolist.size(); i++) {
                        BoBean temp = (BoBean) bolist.get(i);
                        if (i % 2 == 0) {
                            out.println("<tr bgcolor=\"#ffffff\">");
                        } else {
                            out.println("<tr bgcolor=\"#ccffff\">");
                        }
                        out.println("<td>&nbsp;" + temp.getBoname() + "&nbsp;</td>");
                        out.println("<td>&nbsp;" + temp.getHga_partnum() + "&nbsp;</td>");
                        out.println("<td>&nbsp;" + temp.getTsr() + "&nbsp;</td>");
                        out.println("<td>&nbsp;" + temp.getTab() + "&nbsp;</td>");
                        out.println("<td>&nbsp;" + temp.getQty() + "&nbsp;</td>");

                        if(canedit)
                        out.println("<td><input name=\"imgEdit\" id=\"imgEdit\" src=\"./images/buttons/btn_reject.gif\" onclick=\"rejectBo('" + temp.getBoname() + "','"+shelfno+"')\"  border=\"0\"  type=\"image\"> </td>");

                        out.println("</tr>");
                    }
                    out.println("</table>");
                    out.println("</td></tr>");
                    out.println("</table>");
                    out.println("</td>");
                    out.println("</tr>");

                    out.println("</table>");
                    out.println("</td></tr>");
                    out.println("</table>");
                }





            } else if ("loadroominfotable".equalsIgnoreCase(action)) {
                RoomManager rmg = new RoomManager();
                
                List roomlist=rmg.loadAllRoomList();
                out.println("<table bgcolor=\"#CCCCCC\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
                    out.println("<tr><td>");
                    out.println("<table bgcolor=\"#6699CC\" cellpadding=\"3\" cellspacing=\"0\" border=\"0\" width=\"100%\">");

                    out.println("<tr bgcolor=\"#eeeeee\">");
                    out.println("<td>");
                    out.println("<table bgcolor=\"#cccccc\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
                    out.println("<tr><td>");
                    out.println("<table bgcolor=\"#cccccc\" cellpadding=\"3\" cellspacing=\"1\" border=\"0\" width=\"100%\">");

                    out.println("<tr bgcolor=\"#99CC66\">");

                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;Shelf No.&nbsp;</font></td>");
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;Product Name&nbsp;</font></td>");
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;TGA Type&nbsp;</font></td>");
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;Bond Pad&nbsp;</font></td>");
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;Capacity&nbsp;</font></td>");
                    out.println("<td nowrap><font color=\"#ffffff\">&nbsp;ACTION&nbsp;</font></td>");
                     out.println("</tr>");
                for(int i=0;i<roomlist.size();i++){
                   RoomBean roomb=(RoomBean) roomlist.get(i);

                   if (i % 2 == 0) {
                            out.println("<tr bgcolor=\"#ffffff\">");
                        } else {
                            out.println("<tr bgcolor=\"#ccffff\">");
                        }
                   out.println("<td>&nbsp;" + roomb.getShelf_no()+ "&nbsp;</td>");
                   out.println("<td>&nbsp;" + roomb.getProductname()+ "&nbsp;</td>");
                   out.println("<td>&nbsp;" + roomb.getTga_type()+ "&nbsp;</td>");
                   out.println("<td>&nbsp;" + roomb.getBond_pad()+ "&nbsp;</td>");
                   out.println("<td>&nbsp;" +rmg.getBoQtyByShelf(roomb.getShelf_no())+ "&nbsp;</td>");
                   out.println("<td><input name=\"imgEdit\" id=\"imgEdit\" src=\"./images/buttons/btn_edit.gif\" onclick=\"editRoom('" + roomb.getShelf_no()+"')\"  border=\"0\"  type=\"image\"> </td>");

                   out.println("</tr>");
                }
                    out.println("</table>");
                    out.println("</td></tr>");
                    out.println("</table>");
                    out.println("</td>");
                    out.println("</tr>");

                    out.println("</table>");
                    out.println("</td></tr>");
                    out.println("</table>");
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
